package com.university.trade.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailUtil {
	public static final String HOST = "smtp.163.com"; 
	public static final String PROTOCOL = "smtp"; 
	public static final int PORT = 25; 
	
	public static final String SENDER = "yjs5211314wt@163.com"; 
	public static final String SENDERPWD = "mm19920625";//客户端发送邮件授权码，而非邮箱密码 

	public static void main(String[] args) {
		//int i = sendSingleEmail("869893469@qq.com", "邮件发送","测试邮件主题");
		//System.out.println(i);
		Map<String,String> receiverMap = new HashMap<String,String>();
		receiverMap.put("typeToRecipients", "869893469@qq.com");
		receiverMap.put("typeCCRecipients", "2976941154@qq.com");
		receiverMap.put("typeBCCRecipients", "yjs869893469@163.com");
		sendEmailWithRecipientType(receiverMap,"多人邮件发送","多人测试邮件主题");
	}
	/**
	 * 一对一发送邮件方法 
	 * @param receiver 邮件接受者
	 * @param content 邮件内容
	 * @param subject 邮件主题
	 * @return 0：发送失败;1:发送成功
	 */
	public static int sendSingleEmail(String receiver, String content,String subject) {
		Session session = getSession();
		int sendResult = 0;
		try {
			Message msg = new MimeMessage(session);
			// 设置message属性
			msg.setFrom(new InternetAddress(SENDER));
			InternetAddress[] addrs = { new InternetAddress(receiver) };
			//设置邮收件人
			msg.setRecipients(Message.RecipientType.TO, addrs);
			msg.setSubject(subject);
			msg.setSentDate(new Date());
			msg.setContent(content, "text/html;charset=utf-8");
			// 开始发送
			Transport.send(msg);
			sendResult = 1;
			return sendResult;
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return sendResult;
	}
	/**
	 * 带发送方式的发送邮件方法
	 * @param content 邮件的内容
	 * @param subject 邮件主题
	 * @param receiverMap 发送目标的邮箱地址集合,格式如下：<br>
	 * 邮件收件人 ：typeToRecipients ： xxx@xx.com,xxx@xx.com
	 * 邮件抄送人 ：typeCCRecipientsValue ： xxx@xx.com,xxx@xx.com
	 * 邮件密送人 ：typeBCCRecipientsValue ： xxx@xx.com,xxx@xx.com
	 * @return 0：发送失败;1:发送成功
	 * @throws Exception 
	 */
	public static int sendEmailWithRecipientType(Map<String,String> receiverMap, String content,String subject){
		Session session = getSession();
		int sendResult = 0;
		try {
			Message msg = new MimeMessage(session);
			String typeToRecipientsValue = receiverMap.get("typeToRecipients");
			if(typeToRecipientsValue == null){
				throw new Exception("收件人至少一个");
			}
			// 设置message属性
			msg.setFrom(new InternetAddress(SENDER));
			//设置邮件收件人
			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(typeToRecipientsValue));
			//设置邮件抄送人
			String typeCCRecipientsValue = receiverMap.get("typeCCRecipients");
			if(typeCCRecipientsValue != null){
				msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse(typeCCRecipientsValue)); 
			}
			//设置邮件密送人
			String typeBCCRecipientsValue = receiverMap.get("typeBCCRecipientsValue");
			if(typeBCCRecipientsValue != null){
				msg.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(typeBCCRecipientsValue)); 
			}
			msg.setSubject(subject);
			msg.setSentDate(new Date());
			msg.setContent(content, "text/html;charset=utf-8");
			// 开始发送
			Transport.send(msg);
			sendResult = 1;
			return sendResult;
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
			return sendResult;
		}
		return sendResult;
	}
	public static Session getSession() {
		Properties props = new Properties();
		// 设置服务器地址
		props.put("mail.smtp.host",HOST);
		// 设置协议
		props.put("mail.store.protocol",PROTOCOL);
		// 设置端口
		props.put("mail.smtp.port",PORT);
		props.put("mail.smtp.auth",true);

		Authenticator authenticator = new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(SENDER, SENDERPWD);
			}
		};
		Session session = Session.getDefaultInstance(props, authenticator);
		return session;
	}
}
